package com.nbjtjc.safe.api.sys;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.service.sys.SysDeptService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 部门
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @RequestMapping(value = "/find", method = {RequestMethod.POST})
    public Result find(SysDept sysDept, Page page) {
        Result result = new Result();
        try {
            result = sysDeptService.find(sysDept, page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse();
        }
        return result;
    }

    @GetMapping(value = "/findCompanyId/{deptId}")
    public Result findCompanyId(@PathVariable int deptId) {
        Result result = new Result();
        result.setData(sysDeptService.getFatherCompanyId(deptId));
        return result;
    }

    @RequestMapping(value = "/getAllDept", method = {RequestMethod.GET})
    public Result get() {
        Result result = new Result();
        try {
//            result.setData(sysDeptService.getAll());
            result.setData(sysDeptService.getAllConstruct());
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse();
        }
        return result;
    }

    /**
     * 获取当前登录人员机构的所有部门的树状关系
     *
     * @return
     */
    @RequestMapping(value = "/getOrgDept", method = {RequestMethod.POST})
    public Result getOrganizationDept() {
        Result result = new Result();
        try {
//            result = sysDeptService.getOrganiztionDept();
            result.setData(sysDeptService.getLowerCompanyDepts());
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse();
        }
        return result;
    }

    @RequestMapping(value = "/getFartherCompanyDept", method = {RequestMethod.POST})
    public Result getFartherCompanyDept() {
        Result result = new Result();
        result = sysDeptService.getFartherCompanyDept();
        return result;
    }

    @PostMapping(value = "/mvUpDept")
    public Result mvUp(SysDept sysDept) {
        Result result = new Result();
        sysDeptService.mvUp(sysDept);
        return result;
    }

    @PostMapping(value = "/mvDownDept")
    public Result mvDown(SysDept sysDept) {
        Result result = new Result();
        sysDeptService.mvDown(sysDept);
        return result;
    }

    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST})
    public Result add(SysDept sysDept) {
        Result result = new Result();
        if (ObjectUtil.isNull(sysDept)) {
            result.setFalse();
            return result;
        }
        try {
            if (sysDept.getId() != null && sysDept.getId() != 0) {
                sysDeptMapper.updateByPrimaryKeySelective(sysDept);
            } else {
                sysDeptMapper.insertSelective(sysDept);
                //将id作为部门的num
//                sysDept.setNum(sysDept.getId());
                sysDeptMapper.updateByPrimaryKeySelective(sysDept);
            }
            result.setData(sysDept);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public Result delete(int deptId) {
        Result result = new Result();
        try {
            sysDeptMapper.deleteByPrimaryKey(deptId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    
    @RequestMapping(value = "/getAllParentCompanyId",method = {RequestMethod.POST})
    public Result getAllParentCompanyId(Integer deptId){
        Result result=new Result();
        try {
        	List<Integer> deptIds = new ArrayList();
        	if(ObjectUtil.isNull(deptId)){
//        		deptIds.add(0);
//        		result.setData(deptIds);
        		return result;
        	}
        	SysDept sysDept = sysDeptMapper.selectByPrimaryKey(deptId);
        	
        	deptIds = sysDeptService.getAllParentCompanyId(sysDept,deptIds);
        	
//            if(CollectionUtil.isEmpty(deptIds) ){
//        		deptIds.add(0);
//            }
            result.setData(deptIds);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    @RequestMapping(value = "/getAllSubCompanyId",method = {RequestMethod.POST})
    public Result getAllSubCompanyId(Integer deptId){
        Result result=new Result();
        try {
        	List<Integer> deptIds = new ArrayList();
        	if(ObjectUtil.isNull(deptId)){
        		return result;
        	}
        	deptIds.add(deptId);
        	deptIds = sysDeptService.getAllSubCompanyId(deptId,deptIds);
        	
            result.setData(deptIds);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
